<script src="<?php echo URL::base() ?>media/vendor/js/jquery-1.10.2.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-ui-1.10.4.custom.js"></script>
<script src="<?php echo URL::base() ?>media/js/jquery.json-2.4.js"></script>
<link href="<?php echo URL::base() ?>media/vendor/css/ui-lightness/jquery-ui-1.10.4.custom.css" rel="stylesheet">
<style type="text/css">
input{ width:100px;}
.table tr th{text-align:center;}
</style>

<div style='margin-top:10px'>
<span class="lab1">请输入批次号：</span>
<input id="select_pi" type="text" style="width:200px" name="select_pi">

</div>
<div style='margin-top:10px'>
<table  border=1 cellpadding=10 width=30% id='parcel' class="table" style="width:60%">
<tr style=" background-color:#4EA392;color:#fff;">
<th>erp订单号</th>
<th>sku</th>
<th>sku合格数量</th>
<th>sku不合格数量</th>
<th>备注</th>
<th>退件原因选择</th>
</tr>

</table>
</div>
<div style='margin-top:10px'>
<input type='button' value='一键提交' class="commit btn btn-info">
<input type='button' value='追加新增10行数' class="zhuijia btn btn-info">
<input type='hidden' value=0 id="rowbegin">
</div>
<script>
$(document).ready(function(){
$("#select_pi").val('<?php echo (isset($select_pi) && $select_pi!='')?$select_pi:''; ?>');

	var availableTags = [<?php echo $batches;?>];
	$("#select_pi").autocomplete({
       source: availableTags
	});
	$("#select_pi").blur(function(){
		var notice_pi=$("#select_pi").val();
		if(notice_pi=='')return;
		$.ajax({
			type:'post',
			url:'<?php echo url::base(true);?>inventory/returnedparcel/notice_pi',
			dataType:'json',
			data:{select_pi: notice_pi},
			async:false,
			success:function(data)
			{
				//data=JSON.parse(data);
		    	if(data.status=='y'){
		    	}else{
			    	alert(data.info);return;
		    	}
			},
			error:function(){
				alert("错误");return;
			}
		});
	});
	$(".zhuijia").click(function(){
		var newtable = "";
		var rowbegin = parseInt($('#rowbegin').val());
		var rowend   = rowbegin+10;
		$('#rowbegin').attr('value',rowend);
		for(var i=rowbegin;i<rowend;i++){
			newtable +="<tr id='tr_'"+i+">";
			newtable +="<td><input type='text' id='orderid_"+i+"'></td>";
			newtable +="<td><input type='text' id='sku_"+i+"'></td>";
			newtable +="<td><input type='text' id='skuok_"+i+"' value=0></td>";
			newtable +="<td><input type='text' id='skuno_"+i+"' value=0></td>";
			newtable +="<td><input type='text' id='remark_"+i+"'></td>";
			newtable +="<td><select id='reason_"+i+"'><option value=0>无原因</option><option value=1>安检不合格，原单退回</option><option value=2>海外退件，原单退回</option><option value=3>客户退货，已回公司</option></select></td>";
			newtable +="</tr>";
		}
    	$("#parcel").append(newtable);
	});
	$(".commit").click(function(){
		var notice_pi=$("#select_pi").val();
		if(notice_pi==''){
			alert('订单批号不能为空');return;
		}
		if(parseInt($('#rowbegin').val())==0)return;
		$.ajax({
			type:'post',
			url:'<?php echo url::base(true);?>inventory/returnedparcel/notice_pi',
			dataType:'json',
			data:{select_pi: notice_pi},
			async:false,
			success:function(data)
			{
				//data=JSON.parse(data);
		    	if(data.status=='y'){
		    	}else{
			    	alert(data.info);return;
		    	}
			},
			error:function(){
				alert("错误");return;
			}
		});
		var rowbegin = parseInt($('#rowbegin').val());
		var sku_arr = new Array();
		var counter = 0;
		var stamp = 0;
		for(var i=0;i<rowbegin;i++){
			var order = 0;
			var orderid=$('#orderid_'+i).val();
			if(orderid==''){
			}else{
				$.ajax({
					type:'post',
					url:'<?php echo url::base(true);?>inventory/returnedparcel/notice_order',
					dataType:'json',
					data:{order_id: orderid},
					async:false,
					success:function(data)
					{
						//data=JSON.parse(data);
				    	if(data.status=='y'){
				    	}else{
				    		order++;
					    	alert(data.info);return;
				    	}
					},
					error:function(){
						alert("错误");return;
					}
				});
			}
			if(order==1)return;
			var sku_value=$('#sku_'+i).val();
			if(sku_value=='')continue;
    		var skuok = parseInt($('#skuok_'+i).val());
    		var skuno = parseInt($('#skuno_'+i).val());
			var reason = $('#reason_'+i).val();
    		var remark = $('#remark_'+i).val();
			var pi = $("#select_pi").val();

			if(skuok!=0 || skuno!=0){
				if(skuok!=0){
					if(!(/^[0-9]+$/.test(skuok))){
						alert('错误，sku合格数量必须是非负整数');return false;
					}
				}
				if(skuno!=0){
					if(!(/^[0-9]+$/.test(skuno))){
						alert('错误，sku不合格数量必须是非负整数');return false;
					}
				}
			}else{
				alert('请输入sku合格或不合格的数量');return false;
			}

			$.ajax({
				type:'post',
				url:'<?php echo url::base(true);?>inventory/deliverycheck/checksku',
				dataType:'json',
				data:{param: sku_value},
				async:false,
				success:function(data)
				{
					//data=JSON.parse(data);
			    	if(data.status=='y'){
			    	}else{
			    		stamp = 1;
				    	alert('未找到SKU: '+data.sku+' 的对应信息');return;
			    	}
				},
				error:function(){
					alert("错误");return;
				}
			});
			
//			$.post(
//				  "<?php echo URL::base() ?>inventory/deliverycheck/checksku",
//				  {param: sku_value},
//			      function(data){
//			    	data=JSON.parse(data);
//			    	if(data.status=='y'){
//			    	}else{
//			    		flag++;
//			    		$('#stamp').val(1);
//			    		alert($('#stamp').val());
//				    	alert('未找到 '+data.sku+' 的对应信息');return;
//			    	}
//				}
//			);
			sku_arr[counter] = {'sku':sku_value,'orderid':orderid,'skuok':skuok,'skuno':skuno,'remark':remark,'reason':reason,'pi':pi};
			counter ++;
		}
		if(stamp==1)return;
		var sku_json=$.toJSON(sku_arr);
		if(sku_json != ''){
			$.post(
				"<?php echo URL::base() ?>inventory/returnedparcel/shoudong_insert_parcel",
				{skuinfo:sku_json},
				function(data){
					if(data==1){
						alert('提交成功!');
						window.location.reload();
					}
			});
		}
	});
});

$(".erp_order_id").blur(function(){
	var hege_num = $(this).val();
});
</script>
